package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.loan.project.cm.domain.CmCarLicensingAddr;
import com.car.loan.project.cm.mapper.CmCarLicensingAddrMapper;
import com.car.loan.project.cm.service.ICmCarLicensingAddrService;
import com.car.loan.project.system.domain.SysDept;
import com.car.loan.project.system.domain.vo.SysDeptByUserVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 上牌地址信息Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmCarLicensingAddrServiceImpl implements ICmCarLicensingAddrService {

    @Autowired
    private CmCarLicensingAddrMapper cmCarLicensingAddrMapper;

    /**
     * 查询上牌地址信息
     *
     * @param id 上牌地址信息ID
     * @return 上牌地址信息
     */
    @Override
    public CmCarLicensingAddr selectCmCarLicensingAddrById(Long id) {
        return cmCarLicensingAddrMapper.selectById(id);
    }


    /**
     * 查询上牌地址信息列表
     *
     * @param cmCarLicensingAddr 上牌地址信息
     * @return 上牌地址信息
     */
    @Override
    public List<CmCarLicensingAddr> selectCmCarLicensingAddrList(
        CmCarLicensingAddr cmCarLicensingAddr) {
        QueryWrapper<CmCarLicensingAddr> queryWrapper = new QueryWrapper<>(cmCarLicensingAddr);
        return cmCarLicensingAddrMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CmCarLicensingAddr> selectPage(IPage<CmCarLicensingAddr> page,
        QueryWrapper<CmCarLicensingAddr> queryWrapper) {
        return cmCarLicensingAddrMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<CmCarLicensingAddr> buildDeptTreeSelect() {
        CmCarLicensingAddr cmCarLicensingAddrQuery = new CmCarLicensingAddr();
        cmCarLicensingAddrQuery.setStatus(0);
        QueryWrapper<CmCarLicensingAddr> queryWrapper = new QueryWrapper<>(cmCarLicensingAddrQuery);
        List<CmCarLicensingAddr> cmCarLicensingAddrs = cmCarLicensingAddrMapper
            .selectList(queryWrapper);

        //构建树
        List<CmCarLicensingAddr> returnList = new ArrayList<CmCarLicensingAddr>();
        for (Iterator<CmCarLicensingAddr> iterator = cmCarLicensingAddrs.iterator();
            iterator.hasNext(); ) {
            CmCarLicensingAddr t = iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == 0) {
                recursionFn(cmCarLicensingAddrs, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = cmCarLicensingAddrs;
        }

        return returnList;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<CmCarLicensingAddr> list, CmCarLicensingAddr t) {
        // 得到子节点列表
        List<CmCarLicensingAddr> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CmCarLicensingAddr tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<CmCarLicensingAddr> it = childList.iterator();
                while (it.hasNext()) {
                    CmCarLicensingAddr n = (CmCarLicensingAddr) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<CmCarLicensingAddr> getChildList(List<CmCarLicensingAddr> list,
        CmCarLicensingAddr t) {
        List<CmCarLicensingAddr> tlist = new ArrayList<CmCarLicensingAddr>();
        Iterator<CmCarLicensingAddr> it = list.iterator();
        while (it.hasNext()) {
            CmCarLicensingAddr n = (CmCarLicensingAddr) it.next();
            if (n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    private boolean hasChild(List<CmCarLicensingAddr> list, CmCarLicensingAddr t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 新增上牌地址信息
     *
     * @param cmCarLicensingAddr 上牌地址信息
     * @return 结果
     */
    @Override
    public int insertCmCarLicensingAddr(CmCarLicensingAddr cmCarLicensingAddr) {
        cmCarLicensingAddr.setCreateTime(new Date());
        return cmCarLicensingAddrMapper.insert(cmCarLicensingAddr);
    }

    /**
     * 修改上牌地址信息
     *
     * @param cmCarLicensingAddr 上牌地址信息
     * @return 结果
     */
    @Override
    public int updateCmCarLicensingAddr(CmCarLicensingAddr cmCarLicensingAddr) {

        return cmCarLicensingAddrMapper.updateById(cmCarLicensingAddr);
    }

    /**
     * 删除上牌地址信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmCarLicensingAddrByIds(Collection<Long> ids) {
        return cmCarLicensingAddrMapper.deleteBatchIds(ids);
    }

    /**
     * 删除上牌地址信息信息
     *
     * @param id 上牌地址信息ID
     * @return 结果
     */
    @Override
    public int deleteCmCarLicensingAddrById(Long id) {
        return cmCarLicensingAddrMapper.deleteById(id);
    }
}
